//
//  FirstViewController.m
//  jang
//
//  Created by lee han on 03/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "HomeViewController.h"
#import "CRUserData.h"
#import "CommonView.h"
#import "ProductListController.h"
#import "ProductInfoController.h"
#import "MainController.h"
#import "UIImageView+WebCache.h"

@interface HomeViewController (){
    NSMutableArray* m_buttonArray;
}

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [CommonView setNativationBar:self];
    
    _mCycleScrollView.imageURLStringsGroup = [[CRUserData sharedInstance] getScrollImage];
    _mCycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    _mCycleScrollView.delegate = self;
    _mCycleScrollView.titlesGroup = [[CRUserData sharedInstance] getScrollTitle];
    _mCycleScrollView.currentPageDotColor = [UIColor yellowColor]; // 自定义分页控件小圆标颜色
    _mCycleScrollView.placeholderImage = [UIImage imageNamed:@"wenhao"];

    _mSearchVIew.layer.cornerRadius = 8;
    _mSearchVIew.layer.masksToBounds = YES;
    _mSearchVIew.layer.borderWidth = 1;
    _mSearchVIew.layer.borderColor = [[UIColor groupTableViewBackgroundColor] CGColor];
    
    _mInputSearch.delegate = self;
    
    NSInteger bottom = _mExpView.frame.origin.y + _mExpView.frame.size.height + 5;
    
    
    
    m_buttonArray = [[NSMutableArray alloc] init];
    CGRect frame = _mCycleScrollView.frame;
    frame.size.height = frame.size.height * 0.6;
    
    NSArray* homedata = [[CRUserData sharedInstance] getHomeData];
    for (NSInteger i = 0; i < [homedata count]; ++i) {
        NSDictionary* info = [homedata objectAtIndex:i];
        frame.origin.y = bottom;
        
        UIImageView* image = [[UIImageView alloc] initWithFrame:frame];
        NSString* urlstr = [URLIMAGE_PRE stringByAppendingString:[info objectForKey:@"path"]];
        [image sd_setImageWithURL:[NSURL URLWithString:urlstr] placeholderImage:[UIImage imageNamed:@"wenhao.png"]];
        UIButton* button = [[UIButton alloc] initWithFrame:frame];
        button.backgroundColor = [UIColor clearColor];
        [button setTitle:@"" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(onClickHomeData:) forControlEvents:UIControlEventTouchUpInside];
        
        bottom += frame.size.height;
        
        [_mScrollView addSubview:image];
        [_mScrollView addSubview:button];
        [m_buttonArray insertObject:button atIndex:i];
    }
    
    CGSize content = frame.size;
    content.height = bottom;
    _mScrollView.contentSize = content;
    
    NSArray* explist = [[CRUserData sharedInstance] getExpData];
    if([explist count] > 0) {
            _mExpLabel.text = [[explist lastObject] objectForKey:@"title"];
    }
    else {
        _mExpLabel.text = @"无";
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark SDCycleScrollViewDelegate
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSDictionary* cdata = [[CRUserData sharedInstance] getScrollInfo:index];
    [self selectData:cdata];
}
//end SDCycleScrollViewDelegate

-(void)selectData:(NSDictionary*) cdata {
    if([[cdata objectForKey:@"pid"] integerValue] == 0) {
        //分类
        NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:[cdata objectForKey:@"id"], @"categoryid", nil];
        
        [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_ProductList];
    }
    else {
        //单个产品
        NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:[cdata objectForKey:@"pid"], @"pid", nil];
        
        [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_ProductDetail];
    }
}

#pragma mark networkDelegate

- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if (type == NetworkFunctionType_ProductList || type == NetworkFunctionType_BurstSectionList || type == NetworkFunctionType_search){
        if([[response objectForKey:@"status"] integerValue] == 1){
            mProductListData = response;
            [self performSegueWithIdentifier:@"hometopl" sender:self];
        }
        else{
            [CommonView addAlertView:self Text:@"产品列表错误"];
        }
    }
    else if (type == NetworkFunctionType_ProductDetail) {
        if([[response objectForKey:@"status"] integerValue] == 1){
            //goto productinfo view
            mProductBaseInfo = nil;
            mProductReseverInfo = nil;
            mProductBaseInfo = [response objectForKey:@"info"];
            mProductReseverInfo = [response objectForKey:@"data"];
            
            if(mProductBaseInfo != nil && mProductReseverInfo != nil){
                self.tabBarController.tabBar.hidden = YES;
                [self performSegueWithIdentifier:@"hometopinfo" sender:self];
            }
        }
        else{
            [CommonView addAlertView:self Text:@"获取产品错误"];
        }
    }
}
//end networkDelegate

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(textField.text.length == 0){
        return NO;
    }
    [textField resignFirstResponder];
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:textField.text, @"search", nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_search];
    return YES;
}
//end UITextFieldDelegate


- (void)prepareForSegue: (UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"hometopl"]) {
        ProductListController *destination = segue.destinationViewController;
        
        destination.mCategoryID = [[mProductListData objectForKey:@"id"] integerValue];
        destination.mTitle = [mProductListData objectForKey:@"name"];
        destination.mMaxPage = [[mProductListData objectForKey:@"page"] integerValue];
        destination.mListData = [mProductListData objectForKey:@"data"];
        destination.mSearchString = [mProductListData objectForKey:@"search"];
    }
    else if([segue.identifier isEqualToString:@"hometopinfo"]) {
        ProductInfoController *destination = segue.destinationViewController;
        
        destination.mBaseInfo = mProductBaseInfo;
        destination.mReseverInfo = mProductReseverInfo;
    }
    
}

- (IBAction)onClickMyPurse:(id)sender {
    [self performSegueWithIdentifier:@"hometopurse" sender:self];
}

- (IBAction)onClickMyCollect:(id)sender {
    [self performSegueWithIdentifier:@"hometocollect" sender:self];
}
- (IBAction)onClickBurst:(id)sender {
    NSMutableDictionary* data = [[NSMutableDictionary alloc] init];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_BurstSectionList];
}
- (IBAction)onClickService:(id)sender {
    [self performSegueWithIdentifier:@"homttoservice" sender:self];
}
- (IBAction)onClickAllProduct:(id)sender {
    [[MainController sharedInstance] selectCategoryView];
}

-(void)onClickHomeData:(id)sender {
    NSArray* homedata = [[CRUserData sharedInstance] getHomeData];
    for (NSInteger i = 0; i < [m_buttonArray count]; ++i) {
        if (sender == [m_buttonArray objectAtIndex:i]) {
            NSDictionary* cdata = [homedata objectAtIndex:i];
            [self selectData:cdata];
            break;
        }
    }
}

- (IBAction)onClickExp:(id)sender {
    NSArray* explist = [[CRUserData sharedInstance] getExpData];
    if ([explist count] == 0) {
        [CommonView addMessageView:@"暂无数据"];
        return;
    }
    
    [self performSegueWithIdentifier:@"homttoexpl" sender:self];
}
@end
